var express = require('express')
var router = express.Router()
const { getAll, getById, exec } = require('../db/index')

// GET /admins/:id
router.get('/:id',async (req,res) => {
 const {id} = req.params
 let sql = `select * from admins where id = ${id}`
 const data = await getById(sql)
 res.send({
  code:0,
  message:'获取管理员信息成功',
  result:data
 })
})

//PUT /admins/:id
router.put('/:id',async (req,res) =>{
  const {id} = req.params
  const {username,password,email,signature} = req.body
  let sql = `update admins set username='${username}',password='${password}',email='${email}',signature='${signature}' where id = ${id}`
  //console.log(sql)
  await exec(sql)
  res.send({
    code:0,
    message:'修改成功,请重新登录'
  })
})

//POST {username:'',email:'',password:''}
router.post('/',async (req,res) => {
  const {username,email,password} = req.body
  let sql = `insert into admins (username,password,email) values ('${username}','${email}','${password}') `
  console.log(sql)
  const data = await exec(sql)
  console.log(data)
  res.send({
    code:0,
    message:'注册用户成功'
  })
})
module.exports = router
